//
//  TicketDetailViewController.m
//  WeiShangKeProj
//
//  Created by 焱 孙 on 15/5/28.
//  Copyright (c) 2015年 visionet. All rights reserved.
//

#import "TicketDetailViewController.h"
#import "TicketTraceListCell.h"
#import "TicketTraceVo.h"
#import "Utils.h"
#import "TicketTypeVo.h"
#import "UIViewExt.h"
#import "TicketTypeVo.h"
#import "TagVo.h"
#import "CustomTicketViewController.h"
#import "ServerURL.h"
#import "TicketImageCollectionCell.h"
#import "SDWebImageDataSource.h"
#import "KTPhotoScrollViewController.h"
#import "ResizeImage.h"
#import "TicketCommitView.h"
#import "ExtScope.h"
#import "AttachmentVo.h"
#import "DocViewController.h"
#import "DevicePrepaireReportViewController.h"
#import "DevicePrepaireReportTwoViewController.h"
#import "DevicePrepaireReportThreeViewController.h"
#import "DevicePrepaireReportFourViewControlller.h"
#import "ServerProvider.h"

@interface TicketDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,TicketCommitViewDelegate>
{
    UICollectionView *collectionViewImage;
    NSMutableArray *aryPreviewPic;
    
    UIButton *btnReport;
    
    TicketCommitView *acceptCommitView;
    TicketCommitView *transferCommitView;
    TicketCommitView *remoteCommitView;
}

@end

@implementation TicketDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initView];
    [self initData];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    self.bBackRefresh = NO;
    //工单详情
    [self getTicketDetail];
}

- (void)initView
{
    [self setTopNavBarTitle:self.m_ticketVo.strTicketNum];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //服务报告
    btnReport = [Utils buttonWithTitle:@"服务报告" frame:[Utils getNavRightBtnFrame:CGSizeMake(140,76)] target:self action:@selector(enterServiceReport)];
    btnReport.hidden = YES;
    [self setRightBarButton:btnReport];
    
    self.m_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,NAV_BAR_HEIGHT,kScreenWidth,kScreenHeight-NAV_BAR_HEIGHT)];
    self.m_scrollView.autoresizingMask = NO;
    self.m_scrollView.clipsToBounds = YES;
    [self.view addSubview:self.m_scrollView];
    
    //设备序列号
    _lblSerialTitle = [[UILabel alloc]init];
    _lblSerialTitle.text = @"设备序列号";
    _lblSerialTitle.font = [UIFont systemFontOfSize:15];
    _lblSerialTitle.textColor = COLOR(110, 110, 110, 1.0);
    [self.m_scrollView addSubview:_lblSerialTitle];
    
    _lblSerial = [[UILabel alloc]init];
    _lblSerial.font = [UIFont systemFontOfSize:15];
    _lblSerial.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:_lblSerial];
    
    _viewLineSerial = [[UIView alloc]init];
    _viewLineSerial.backgroundColor = COLOR(183, 183, 183, 1.0);
    [self.m_scrollView addSubview:_viewLineSerial];
    
    //联系人
    _lblContactTitle = [[UILabel alloc]init];
    _lblContactTitle.text = @"联系人";
    _lblContactTitle.font = [UIFont systemFontOfSize:15];
    _lblContactTitle.textColor = COLOR(110, 110, 110, 1.0);
    [self.m_scrollView addSubview:_lblContactTitle];
    
    _lblContact = [[UILabel alloc]init];
    _lblContact.font = [UIFont systemFontOfSize:15];
    _lblContact.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:_lblContact];
    
    _viewLineContact = [[UIView alloc]init];
    _viewLineContact.backgroundColor = COLOR(183, 183, 183, 1.0);
    [self.m_scrollView addSubview:_viewLineContact];
    
    //手机
    _lblMobileTitle = [[UILabel alloc]init];
    _lblMobileTitle.text = @"手机";
    _lblMobileTitle.font = [UIFont systemFontOfSize:15];
    _lblMobileTitle.textColor = COLOR(110, 110, 110, 1.0);
    [self.m_scrollView addSubview:_lblMobileTitle];
    
    _btnMobile = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnMobile.layer.cornerRadius = 4;
    _btnMobile.layer.masksToBounds = YES;
    [_btnMobile addTarget:self action:@selector(bottomButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_btnMobile setBackgroundImage:[Common getImageWithColor:COLOR(240, 240, 240, 1.0)] forState:UIControlStateHighlighted];
    [self.m_scrollView addSubview:_btnMobile];
    
    _lblMobile = [[UILabel alloc]init];
    _lblMobile.font = [UIFont systemFontOfSize:15];
    _lblMobile.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:_lblMobile];
    
    _viewLineMobile = [[UIView alloc]init];
    _viewLineMobile.backgroundColor = COLOR(183, 183, 183, 1.0);
    [self.m_scrollView addSubview:_viewLineMobile];
    
    //单位
    _lblUnitTitle = [[UILabel alloc]init];
    _lblUnitTitle.text = @"单位";
    _lblUnitTitle.font = [UIFont systemFontOfSize:15];
    _lblUnitTitle.textColor = COLOR(110, 110, 110, 1.0);
    [self.m_scrollView addSubview:_lblUnitTitle];
    
    _lblUnit = [[UILabel alloc]init];
    _lblUnit.font = [UIFont systemFontOfSize:15];
    _lblUnit.textColor = COLOR(51, 51, 51, 1.0);
    _lblUnit.numberOfLines = 0;
    _lblUnit.lineBreakMode = NSLineBreakByWordWrapping;
    _lblUnit.textAlignment = NSTextAlignmentLeft;
    [self.m_scrollView addSubview:_lblUnit];
    
    _viewLineUnit = [[UIView alloc]init];
    _viewLineUnit.backgroundColor = COLOR(183, 183, 183, 1.0);
    [self.m_scrollView addSubview:_viewLineUnit];
    
    //上门时间
    _lblDateTitle = [[UILabel alloc]init];
    _lblDateTitle.text = @"上门时间";
    _lblDateTitle.font = [UIFont systemFontOfSize:15];
    _lblDateTitle.textColor = COLOR(110, 110, 110, 1.0);
    [self.m_scrollView addSubview:_lblDateTitle];
    
    _lblDate = [[UILabel alloc]init];
    _lblDate.font = [UIFont systemFontOfSize:15];
    _lblDate.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:_lblDate];
    
    _viewLineDate = [[UIView alloc]init];
    _viewLineDate.backgroundColor = COLOR(183, 183, 183, 1.0);
    [self.m_scrollView addSubview:_viewLineDate];
    
    //描述
    _lblContent = [[UILabel alloc]init];
    _lblContent.font = [UIFont systemFontOfSize:15];
    _lblContent.textColor = COLOR(51, 51, 51, 1.0);
    [self.m_scrollView addSubview:_lblContent];
    
    //工单图片
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize=CGSizeMake(77,77);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    collectionViewImage = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionViewImage.dataSource = self;
    collectionViewImage.delegate = self;
    collectionViewImage.backgroundColor = [UIColor clearColor];
    collectionViewImage.showsVerticalScrollIndicator = NO;
    [self.m_scrollView addSubview:collectionViewImage];
    [collectionViewImage registerClass:[TicketImageCollectionCell class] forCellWithReuseIdentifier:@"TicketImageCollectionCell"];
    
    _viewLineContent = [[UIView alloc]init];
    _viewLineContent.backgroundColor = COLOR(183, 183, 183, 1.0);
    [self.m_scrollView addSubview:_viewLineContent];
    
    //工单操作记录
    self.tableViewRecord = [[UITableView alloc]init];
    self.tableViewRecord.dataSource = self;
    self.tableViewRecord.delegate = self;
    self.tableViewRecord.scrollEnabled = NO;
    self.tableViewRecord.scrollsToTop = NO;
    self.tableViewRecord.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.m_scrollView addSubview:self.tableViewRecord];
    
    //底部按钮
    self.viewBottom = [[UIView alloc]initWithFrame:CGRectMake(-0.5, kScreenHeight-50, kScreenWidth+1, 50.5)];
    self.viewBottom.layer.masksToBounds = YES;
    self.viewBottom.layer.borderColor = COLOR(138, 138, 138, 1.0).CGColor;
    self.viewBottom.layer.borderWidth = 0.5;
    self.viewBottom.backgroundColor = COLOR(222, 222, 222, 1.0);
    [self.view addSubview:_viewBottom];
    
    UIImageView *imgViewTabLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1.5)];
    imgViewTabLine.image = [[UIImage imageNamed:@"tab_line"]stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    [self.viewBottom addSubview:imgViewTabLine];

    self.lblStatus = [[UILabel alloc] initWithFrame:CGRectZero];
    self.lblStatus.font = [UIFont systemFontOfSize:13];
    self.lblStatus.lineBreakMode = NSLineBreakByWordWrapping;
    self.lblStatus.textColor = [UIColor whiteColor];
    self.lblStatus.textAlignment = NSTextAlignmentCenter;
    [self.lblStatus.layer setBorderWidth:0];
    [self.lblStatus.layer setCornerRadius:9];
    self.lblStatus.layer.borderColor = [[UIColor clearColor] CGColor];
    [self.lblStatus.layer setMasksToBounds:YES];
    [self.viewBottom addSubview:self.lblStatus];
    
    //接受（开始维修）
    self.btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnStart setTitleColor:COLOR(136, 136, 136, 1.0) forState:UIControlStateHighlighted];
    [self.btnStart setBackgroundImage:[Common getImageWithColor:THEME_COLOR] forState:UIControlStateNormal];
    [self.btnStart.titleLabel setFont:[UIFont fontWithName:APP_FONT_NAME size:14.0]];
    [self.btnStart setTitle:@"接受" forState:UIControlStateNormal];
    self.btnStart.layer.cornerRadius = 5;
    self.btnStart.layer.masksToBounds = YES;
    [self.btnStart addTarget:self action:@selector(bottomButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBottom addSubview:self.btnStart];
    
    //转移
    self.btnTransfer = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnTransfer setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnTransfer setTitleColor:COLOR(136, 136, 136, 1.0) forState:UIControlStateHighlighted];
    [self.btnTransfer setBackgroundImage:[Common getImageWithColor:THEME_COLOR] forState:UIControlStateNormal];
    [self.btnTransfer.titleLabel setFont:[UIFont fontWithName:APP_FONT_NAME size:14.0]];
    [self.btnTransfer setTitle:@"转移" forState:UIControlStateNormal];
    self.btnTransfer.layer.cornerRadius = 5;
    self.btnTransfer.layer.masksToBounds = YES;
    [self.btnTransfer addTarget:self action:@selector(bottomButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBottom addSubview:self.btnTransfer];
    
    //远程完成
    self.btnRemoteDone = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnRemoteDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnRemoteDone setTitleColor:COLOR(136, 136, 136, 1.0) forState:UIControlStateHighlighted];
    [self.btnRemoteDone setBackgroundImage:[Common getImageWithColor:THEME_COLOR] forState:UIControlStateNormal];
    [self.btnRemoteDone.titleLabel setFont:[UIFont fontWithName:APP_FONT_NAME size:14.0]];
    [self.btnRemoteDone setTitle:@"远程完成" forState:UIControlStateNormal];
    self.btnRemoteDone.layer.cornerRadius = 5;
    self.btnRemoteDone.layer.masksToBounds = YES;
    [self.btnRemoteDone addTarget:self action:@selector(bottomButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBottom addSubview:self.btnRemoteDone];
    
    //完成
    self.btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnDone setTitleColor:COLOR(136, 136, 136, 1.0) forState:UIControlStateHighlighted];
    [self.btnDone setBackgroundImage:[Common getImageWithColor:THEME_COLOR] forState:UIControlStateNormal];
    [self.btnDone.titleLabel setFont:[UIFont fontWithName:APP_FONT_NAME size:14.0]];
    [self.btnDone setTitle:@"完成" forState:UIControlStateNormal];
    self.btnDone.layer.cornerRadius = 5;
    self.btnDone.layer.masksToBounds = YES;
    [self.btnDone addTarget:self action:@selector(bottomButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBottom addSubview:self.btnDone];
    
    //提交操作弹出的提交视图
    
    //1.接受【选择预计上门时间】
    acceptCommitView = [[TicketCommitView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) type:0 delegate:self];
    
    //2.转移【选择接受的工程师、备注】
    transferCommitView = [[TicketCommitView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) type:1 delegate:self];
    
    //3.远程完成 [必须填写备注]
    remoteCommitView = [[TicketCommitView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) type:2 delegate:self];
}

- (void)refreshView
{
    [self setTopNavBarTitle:self.m_ticketVo.strTicketNum];
    
    //服务报告按钮
    NSInteger nState = [BusinessCommon getTicketIntStateByStr:self.m_ticketVo.strStatus];
    if (nState == 2 || nState == 3 || nState == 5)
    {
        btnReport.hidden = NO;
    }
    else
    {
        btnReport.hidden = YES;
    }
    
    CGFloat fHeight = 0;
    //设备序列号
    _lblSerialTitle.frame = CGRectMake(10, fHeight, 80, 40);
    
    _lblSerial.text = self.m_ticketVo.equipmentVo.strSeq;
    _lblSerial.frame = CGRectMake(_lblSerialTitle.right+15, fHeight, kScreenWidth-(_lblSerialTitle.right+15), 40);
    fHeight += _lblSerial.height;
    
    self.viewLineSerial.frame = CGRectMake(10, fHeight, kScreenWidth-10, 0.5);
    fHeight += 0.5;
    
    //联系人
    _lblContactTitle.frame = CGRectMake(10, fHeight, 100, 40);
    
    _lblContact.text = self.m_ticketVo.strLinkMan;
    _lblContact.frame = CGRectMake(_lblSerialTitle.right+15, fHeight, kScreenWidth-(_lblSerialTitle.right+15), 40);
    fHeight += _lblContact.height;
    
    _viewLineContact.frame = CGRectMake(10, fHeight, kScreenWidth-10, 0.5);
    fHeight += 0.5;
    
    //手机
    _lblMobileTitle.frame = CGRectMake(10, fHeight, 100, 40);
    
    _lblMobile.text = self.m_ticketVo.strMobile;
    _lblMobile.frame = CGRectMake(_lblSerialTitle.right+15, fHeight, kScreenWidth-(_lblSerialTitle.right+15), 40);
    _btnMobile.frame = _lblMobile.frame;
    _btnMobile.left = _btnMobile.left - 10;
    fHeight += _lblMobile.height;
    
    _viewLineMobile.frame = CGRectMake(10, fHeight, kScreenWidth-10, 0.5);
    fHeight += 0.5;
    
    //单位
    _lblUnitTitle.frame = CGRectMake(10, fHeight, 100, 40);
    
    _lblUnit.text = self.m_ticketVo.equipmentVo.strHospitalName;
    CGSize sizeHospital = [Common getStringSize:_lblUnit.text font:_lblUnit.font bound:CGSizeMake(kScreenWidth-(_lblSerialTitle.right+15), MAXFLOAT) lineBreakMode:_lblUnit.lineBreakMode];
    if (sizeHospital.height > 20)
    {
        sizeHospital.height += 25;
    }
    else
    {
        sizeHospital.height = 40;
    }
    _lblUnit.frame = CGRectMake(_lblSerialTitle.right+15, fHeight, kScreenWidth-(_lblSerialTitle.right+15), sizeHospital.height);
    fHeight += _lblUnit.height;
    
    _viewLineUnit.frame = CGRectMake(10, fHeight, kScreenWidth-10, 0.5);
    fHeight += 0.5;
    
    //上门时间
    if (nState>=2 && nState <= 5)
    {
        _lblDateTitle.frame = CGRectMake(10, fHeight, 100, 40);
        
        _lblDate.text = self.m_ticketVo.strServiceTime;
        _lblDate.frame = CGRectMake(_lblSerialTitle.right+15, fHeight, kScreenWidth-(_lblSerialTitle.right+15), 40);
        fHeight += _lblDate.height;
        
        _viewLineDate.frame = CGRectMake(10, fHeight, kScreenWidth-10, 0.5);
        fHeight += 0.5;
        
        _lblDateTitle.hidden = NO;
        _lblDate.hidden = NO;
        _viewLineDate.hidden = NO;
    }
    else
    {
        _lblDateTitle.hidden = YES;
        _lblDate.hidden = YES;
        _viewLineDate.hidden = YES;
    }
    
    //描述
    fHeight += 10;
    _lblContent.text = self.m_ticketVo.strContent;
    CGSize sizeLbl = [Common getStringSize:_lblContent.text font:_lblContent.font bound:CGSizeMake(kScreenWidth-20, MAXFLOAT) lineBreakMode:_lblContent.lineBreakMode];
    _lblContent.frame = CGRectMake(10, fHeight, kScreenWidth-20, sizeLbl.height);
    fHeight += _lblContent.height+5;
    
    //图片列表
    CGFloat fCollectionH = 0.0;
    int nColumnNum = (kScreenWidth-12)/77;
    if (self.m_ticketVo.aryAttachment.count%nColumnNum == 0)
    {
        //正好整除
        fCollectionH = (self.m_ticketVo.aryAttachment.count/nColumnNum)*77;
    }
    else
    {
        //有余数
        fCollectionH = (self.m_ticketVo.aryAttachment.count/nColumnNum+1)*77;
    }
    
    collectionViewImage.frame = CGRectMake(6, fHeight+10, kScreenWidth-12, fCollectionH);
    [collectionViewImage reloadData];
    fHeight += fCollectionH+20;
    
    _viewLineContent.frame = CGRectMake(10, fHeight, kScreenWidth-10, 0.5);
    fHeight += 0.5;

    //ticket record list
    self.tableViewRecord.frame = CGRectMake(0, fHeight+1, kScreenWidth, self.tableViewRecord.contentSize.height);
    fHeight +=30+self.tableViewRecord.contentSize.height + 50;//30:table header view
    
    //scrollview
    fHeight = (fHeight>self.m_scrollView.height)?fHeight:(self.m_scrollView.height+0.5);
    [self.m_scrollView setContentSize:CGSizeMake(kScreenWidth,fHeight)];
    
    //状态
    [self updateBottomBar];
}

- (void)updateBottomBar
{
    //如果已经转移则隐藏底部button 视图
    if([self.m_ticketVo.strUserID isEqualToString:[Common getCurrentUserVo].strUserID])
    {
        self.viewBottom.hidden = NO;
    }
    else
    {
        self.viewBottom.hidden = YES;
        btnReport.hidden = YES;
    }

    NSInteger nState = [BusinessCommon getTicketIntStateByStr:self.m_ticketVo.strStatus];
    self.lblStatus.backgroundColor = [BusinessCommon getTicketColorByStatus:self.m_ticketVo.strStatus];
    self.lblStatus.text = self.m_ticketVo.strStatusName;
    CGSize sizeLbl = [Common getStringSize:self.lblStatus.text font:self.lblStatus.font bound:CGSizeMake(MAXFLOAT, 20) lineBreakMode:self.lblStatus.lineBreakMode];
    self.lblStatus.frame = CGRectMake(15, 15, sizeLbl.width+10, 20);
    
    CGFloat fLeft = self.lblStatus.right + 15;
    if (nState < 2) //已分配
    {
        //接受
        self.btnStart.frame = CGRectMake(fLeft, 9, 57, 32);
        fLeft += self.btnStart.width+15;
        
        //转移
        self.btnTransfer.frame = CGRectMake(fLeft, 9, 57, 32);
        fLeft += self.btnTransfer.width+15;
        
        //远程完成
        self.btnRemoteDone.frame = CGRectMake(fLeft, 9, 88, 32);
    }
    else
    {
        self.btnStart.hidden = YES;
        self.btnTransfer.hidden = YES;
        self.btnRemoteDone.hidden = YES;
        if (nState == 2)
        {
            //正在维修，显示完成按钮
            self.btnDone.frame = CGRectMake(fLeft, 9, 60, 32);
        }
        else
        {
            //已经处理完成,隐藏完成按钮
            self.btnDone.hidden = YES;
        }
    }
}

//工单详情
-(void)getTicketDetail
{
    [self isHideActivity:NO];
    [ServerProvider getTicketDetail:self.m_ticketVo.strID result:^(ServerReturnInfo *retInfo) {
        [self isHideActivity:YES];
        if (retInfo.bSuccess)
        {
            self.m_ticketVo = retInfo.data;
            
            [self setPreviewPicArray];
            self.strTypeID = self.m_ticketVo.strTypeID;
           
            [self.tableViewRecord reloadData];
            
            [self refreshView];
        }
        else
        {
            [Common tipAlert:retInfo.strErrorMsg];
        }
    }];
}

- (void)setPreviewPicArray
{
    aryPreviewPic = [NSMutableArray array];
    for (AttachmentVo *attachmentVo in self.m_ticketVo.aryAttachment)
    {
        if(![attachmentVo.strType isEqualToString:@"audio"])
        {
            attachmentVo.nImageIndex = aryPreviewPic.count;
            
            //取大图
            NSURL *urlMax,*urlMin;
            urlMax = [NSURL URLWithString:attachmentVo.strAttachmentURL];
            urlMin = urlMax;
            
            if (urlMax == nil || urlMin == nil)
            {
                continue;
            }
            
            NSArray *ary = @[urlMax,urlMin];
            [aryPreviewPic addObject:ary];
        }
    }
}

//服务报告
- (void)enterServiceReport
{
    NSString *strServiceTypeName = self.m_ticketVo.strServiceTypeName;
    
    
    if ([strServiceTypeName isEqual: @"维修"]||[strServiceTypeName isEqual:@"其他"]) {
//    if ([strServiceTypeName isEqual:@"异常结果处理"]){
        DevicePrepaireReportViewController *devicePreapirReprotVC = [[DevicePrepaireReportViewController alloc]init];
        devicePreapirReprotVC.strHospitalName = self.m_ticketVo.equipmentVo.strHospitalName;
        devicePreapirReprotVC.strLinkMan = self.m_ticketVo.strLinkMan;
        devicePreapirReprotVC.strMobile = self.m_ticketVo.strTelephone;
        devicePreapirReprotVC.strTicketNum = self.m_ticketVo.equipmentVo.strTypeID;
        devicePreapirReprotVC.strEqseq = self.m_ticketVo.strTicketNum;
        devicePreapirReprotVC.serviceId = self.m_ticketVo.strID;
        [self.navigationController pushViewController:devicePreapirReprotVC animated:YES];

    }else if ([strServiceTypeName isEqual:@"异常结果处理"]){
//    else if ([strServiceTypeName isEqual: @"维修"]||[strServiceTypeName isEqual:@"其他"]) {
        DevicePrepaireReportTwoViewController * devicePrepaireReportTwoVC =[[DevicePrepaireReportTwoViewController alloc]init];
        devicePrepaireReportTwoVC.strHospitalName = self.m_ticketVo.equipmentVo.strHospitalName;
        devicePrepaireReportTwoVC.strLinkMan = self.m_ticketVo.strLinkMan;
        devicePrepaireReportTwoVC.strMobile = self.m_ticketVo.strMobile;
        devicePrepaireReportTwoVC.strEqseq = self.m_ticketVo.strTicketNum;
        devicePrepaireReportTwoVC.serviceId = self.m_ticketVo.strID;;
        devicePrepaireReportTwoVC.strTypeID = self.m_ticketVo.equipmentVo.strTypeID;
        [self.navigationController pushViewController:devicePrepaireReportTwoVC animated:YES];
        
    }else if ([strServiceTypeName isEqual:@"新装机培训"]){
        DevicePrepaireReportThreeViewController *devicePrepaireReportThreeVC = [[DevicePrepaireReportThreeViewController alloc]init];
        [self.navigationController pushViewController:devicePrepaireReportThreeVC animated:YES];
    }else{
        DevicePrepaireReportFourViewControlller *devicePrepaireReportFourVC = [[DevicePrepaireReportFourViewControlller alloc]init];
        [self.navigationController pushViewController:devicePrepaireReportFourVC animated:YES];
    }
        
    }

//底部按钮事件
- (void)bottomButtonAction:(UIButton*)sender
{
    
//    [self.view addSubview:acceptCommitView];
//    [acceptCommitView refreshView];
    
//    [self.view addSubview:remoteCommitView];
//    [remoteCommitView refreshView];
    
//    [self.view addSubview:transferCommitView];
//    [transferCommitView refreshView];
//    return;
    
    if (sender == self.btnStart)
    {
        //接受
        [self.view addSubview:acceptCommitView];
        [acceptCommitView refreshView];
    }
    else if (sender == self.btnTransfer)
    {
        //转移
        [self.view addSubview:transferCommitView];
        [transferCommitView refreshView];
    }
    else if (sender == self.btnRemoteDone)
    {
        //远程完成
        [self.view addSubview:remoteCommitView];
        [remoteCommitView refreshView];
    }
    else if (sender == self.btnDone)
    {
        //完成
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"更改后不可再修改" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertView.tag = 1001;
        [alertView show];
    }
    else if (sender == self.btnMobile)
    {
        //拨打电话
        if(self.m_ticketVo.strMobile.length > 0)
        {
            UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:nil message:@"您确定要拨号？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            alertView.tag = 1002;
            [alertView show];
        }
    }
}

- (void)backButtonClicked
{
    if (self.bBackRefresh && self.refreshTicketBlock != nil)
    {
        //refresh list view
        self.refreshTicketBlock();
    }
    
    [super backButtonClicked];
}

#pragma mark - TicketCommitViewDelegate
- (void)completedAction:(NSInteger)nType data:(TicketCommitVo *)ticketCommitVo
{
    if (nType == 0)
    {
        //1.接受【选择预计上门时间（可选）】
        if(ticketCommitVo != nil)
        {
            ticketCommitVo.strTicketID = self.m_ticketVo.strID;
            ticketCommitVo.strStatus = @"C";
            [self isHideActivity:NO];
            [ServerProvider updateTicketInfo:ticketCommitVo result:^(ServerReturnInfo *retInfo) {
                [self isHideActivity:YES];
                if (retInfo.bSuccess)
                {
                    [acceptCommitView removeFromSuperview];
                    self.bBackRefresh = YES;
                    self.m_ticketVo.strStatus = @"C";
                    [self updateBottomBar];
    
                    [self getTicketDetail];
                }
                else
                {
                    [Common tipAlert:retInfo.strErrorMsg];
                }
            }];
        }
        else
        {
            [acceptCommitView removeFromSuperview];
        }
    }
    else if(nType == 1)
    {
        //2.转移【选择接受的工程师、备注】
        if(ticketCommitVo != nil)
        {
            ticketCommitVo.strTicketID = self.m_ticketVo.strID;
            [self isHideActivity:NO];
            [ServerProvider transferTicket:ticketCommitVo result:^(ServerReturnInfo *retInfo) {
                [self isHideActivity:YES];
                if (retInfo.bSuccess)
                {
                    [transferCommitView removeFromSuperview];
                    [Common bubbleTip:@"成功转移该工单" andView:self.view];
                    self.bBackRefresh = YES;
                    [self backButtonClicked];
                }
                else
                {
                    [Common tipAlert:retInfo.strErrorMsg];
                }
            }];
        }
        else
        {
            [transferCommitView removeFromSuperview];
        }
    }
    else
    {
        //3.远程完成 [必须填写备注]
        if(ticketCommitVo != nil)
        {
            ticketCommitVo.strTicketID = self.m_ticketVo.strID;
            ticketCommitVo.strStatus = @"R";
            [self isHideActivity:NO];
            [ServerProvider updateTicketInfo:ticketCommitVo result:^(ServerReturnInfo *retInfo) {
                [self isHideActivity:YES];
                if (retInfo.bSuccess)
                {
                    [remoteCommitView removeFromSuperview];
                    self.bBackRefresh = YES;
                    self.m_ticketVo.strStatus = @"R";
                    [self updateBottomBar];
                    
                    [self getTicketDetail];
                }
                else
                {
                    [Common tipAlert:retInfo.strErrorMsg];
                }
            }];
        }
        else
        {
            [remoteCommitView removeFromSuperview];
        }
    }
}

#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 1001)
    {
        if (buttonIndex==1)
        {
            //完成
            TicketCommitVo *ticketCommitVo = [[TicketCommitVo alloc]init];
            ticketCommitVo.strTicketID = self.m_ticketVo.strID;
            ticketCommitVo.strStatus = @"E";
            [self isHideActivity:NO];
            [ServerProvider updateTicketInfo:ticketCommitVo result:^(ServerReturnInfo *retInfo) {
                [self isHideActivity:YES];
                if (retInfo.bSuccess)
                {
                    self.bBackRefresh = YES;
                    self.m_ticketVo.strStatus = @"E";
                    [self updateBottomBar];
                    
                    [self getTicketDetail];
                }
                else
                {
                    [Common tipAlert:retInfo.strErrorMsg];
                }
            }];
        }
    }
    else
    {
        if (buttonIndex==1)
        {
            NSString *strPhoneNumber = [@"tel://" stringByAppendingString:self.m_ticketVo.strMobile];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strPhoneNumber]];
        }
    }
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.m_ticketVo.aryTrace count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TicketTraceListCell";
    TicketTraceListCell *cell = (TicketTraceListCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[TicketTraceListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    TicketTraceVo *ticketTraceVo = self.m_ticketVo.aryTrace[indexPath.row];
    [cell initWithData:ticketTraceVo];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [TicketTraceListCell calculateCellHeight:self.m_ticketVo.aryTrace[indexPath.row]];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreenWidth-10, 29.5)];
    label.textColor = COLOR(51,51,51,1.0);
    label.font = [UIFont systemFontOfSize:16];
    label.text = @"跟踪记录";
    [headerView addSubview:label];
    
    UIView *viewLine = [[UIView alloc]initWithFrame:CGRectMake(0, 29.5, kScreenWidth, 0.5)];
    viewLine.backgroundColor = COLOR(204, 204, 204, 1.0);
    [headerView addSubview:viewLine];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

#pragma mark - UICollectionViewDataSource UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TicketImageCollectionCell";
    TicketImageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    [cell initWithAttachment:self.m_ticketVo.aryAttachment[indexPath.row]];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.m_ticketVo.aryAttachment.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    AttachmentVo *attchmentVo = self.m_ticketVo.aryAttachment[indexPath.row];
    if ([attchmentVo.strType isEqualToString:@"audio"])
    {
        DocViewController *docViewController = [[DocViewController alloc] init];
        docViewController.strDocName = @"音频";
        docViewController.urlFile = [NSURL URLWithString:attchmentVo.strAttachmentURL];
        [self.navigationController pushViewController:docViewController animated:YES];
    }
    else
    {
        SDWebImageDataSource *dataSource = [[SDWebImageDataSource alloc] init];
        dataSource.images_ = aryPreviewPic;
        KTPhotoScrollViewController *photoScrollViewController = [[KTPhotoScrollViewController alloc] initWithDataSource:dataSource andStartWithPhotoAtIndex:attchmentVo.nImageIndex];
        photoScrollViewController.bShowImageNumBarBtn = YES;
        [self.navigationController pushViewController:photoScrollViewController animated:YES];
    }
}

@end
